package com.dcy.znoa.controller.sys;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.dcy.znoa.common.annotation.AuthenticationAPI;
import com.dcy.znoa.common.controller.BaseController;
import com.dcy.znoa.common.exception.BusinessException;
import com.dcy.znoa.common.util.pojo.JsonCommon;
import com.dcy.znoa.common.util.pojo.JsonForList;
import com.dcy.znoa.dao.sys.AppVersionDao;
import com.dcy.znoa.domain.sys.AppVersion;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.lang.reflect.InvocationTargetException;
import java.util.List;

/**
 * @author pengyu
 */
@Controller
@RequestMapping("appVersion") //功能较少，所以没有service
public class AppVersionController extends BaseController {
    @Autowired
    AppVersionDao appVersionDao;

    @AuthenticationAPI
    @ResponseBody
    @RequestMapping("/selectList")
    public JsonForList selectList(AppVersion appVersion) throws BusinessException, InvocationTargetException, IllegalAccessException {
        EntityWrapper ew = new EntityWrapper<AppVersion>();
        ew.setEntity(appVersion);
//        ew.eq("os_type",appVersion.getOsType());
//        ew.eq("valid_flag",appVersion.getValidFlag());
        List list = appVersionDao.selectList(ew);
        return new JsonForList(list);
    }

    @AuthenticationAPI
    @ResponseBody
    @RequestMapping("/selectNewest")
    public JsonCommon selectNewest(Integer osType) throws BusinessException, InvocationTargetException, IllegalAccessException {
        if(osType==null){
            return new JsonCommon("404","osType不能为空");
        }
        EntityWrapper ew = new EntityWrapper<AppVersion>();
        ew.last(" where os_type = " + osType + " and  valid_flag = 1 order by version desc  limit 1");
        List list = appVersionDao.selectList(ew);
        if (!list.isEmpty()) {
            return new JsonCommon(list.get(0));
        }
        return null;
    }

}
